﻿/****************************************************************************
Copyright (c) 2011-2013,WebJet Business Division,CYOU

http://www.genesis-3d.com.cn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using System;
using System.Runtime.CompilerServices;
using ScriptRuntime;
namespace ScriptRuntime
{
    public class SunShaft : ScriptableClass
    {
        private MaterialInstance SunShaftMat0 = null;
        private MaterialInstance SunShaftMat1 = null;
        private MaterialInstance SunShaftMat2 = null;

        private RenderToTexture buffer1 = null;
        private RenderToTexture buffer2 = null;

		public Vector4 SunPos = new Vector4(30.000000f,60.387765f,0.0f,1.0f);
        public Color32 SunColor = new Color32(204, 204, 153, 255);
		public float Length = 0.76319116f;
        private void Init()
        {
            SunShaftMat0 = GraphicSystem.CreateMaterial("asset:PostEffects/Shader/PostEffect_SunShaft0.shader");
            SunShaftMat1 = GraphicSystem.CreateMaterial("asset:PostEffects/Shader/PostEffect_SunShaft1.shader");
            SunShaftMat2 = GraphicSystem.CreateMaterial("asset:PostEffects/Shader/PostEffect_SunShaft2.shader");

            int width = GraphicSystem.GetWidth();
            int height = GraphicSystem.GetHeight();
            Vector4 color = Vector4.One;

            buffer1 = new RenderToTexture();
            buffer1.Setup( width , height , PixelFormat.X8R8G8B8 , ClearFlag.ClearAll , ref color , false );

            buffer2 = new RenderToTexture();
            buffer2.Setup( width , height , PixelFormat.X8R8G8B8 , ClearFlag.ClearAll , ref color , false );
        }

		private void Update(Vector4 color,Vector4 pos)
		{
			SunShaftMat0.SetValue("SunShafts_SunPos",pos);
			SunShaftMat0.SetValue("SunShafts_SunCol",color);

            SunShaftMat1.SetValue("SunShafts_SunPos", pos);
            SunShaftMat1.SetValue("SunShafts_SunCol", color);

            SunShaftMat2.SetValue("SunShafts_SunPos", pos);
            SunShaftMat2.SetValue("SunShafts_SunCol", color);
		}

        public override bool OnRenderPostEffect(CameraComponent sender, RenderToTexture source, RenderToTexture destination)
        {
			CameraComponent mainCam = GraphicSystem.GetSceneCamera();
			Vector4 screenpos = Matrix44.TransformVector4(mainCam.ViewProj,SunPos);

			if(screenpos.Z<0.0f)
			{
				GraphicSystem.BlitImage( source, destination, null, 0 );
				return true;
			}
            if (Length <= 0.0f)
            {
                Length = 0.00000001f;
            }
            Vector4 sunShaftsParam = new Vector4(0.0f, 1 / Length, 0.0f, 0.0f);
            if (SunShaftMat0 == null
                ||SunShaftMat1 == null
                || SunShaftMat2 == null)
            {
                Init();
            }

            Vector4 color = new Vector4(SunColor.R / 255.0f, SunColor.G / 255.0f, SunColor.B / 255.0f, SunColor.A / 255.0f);
            Update(color, SunPos);

			GraphicSystem.BlitImage( source, buffer1, SunShaftMat0, 0 );

			sunShaftsParam.X = 0.1f;
			SunShaftMat1.SetValue("PI_sunShaftsParam1",sunShaftsParam);
			GraphicSystem.BlitImage( buffer1, buffer2, SunShaftMat1, 0 );

			sunShaftsParam.X = 0.25f;
			SunShaftMat1.SetValue("PI_sunShaftsParam1",sunShaftsParam);
            GraphicSystem.BlitImage( buffer2, buffer1, SunShaftMat1, 0 );

			sunShaftsParam.X = 0.01f;
			SunShaftMat1.SetValue("PI_sunShaftsParam1",sunShaftsParam);
            GraphicSystem.BlitImage( buffer1, buffer2, SunShaftMat1, 0 );

			SunShaftMat2.SetTexture("p4RT",buffer2);
			GraphicSystem.BlitImage( source, destination, SunShaftMat2, 0 );
            return true;
        }
    };
}



                                                                                                